/* Copyright 2017, 2018, Gabriel Czernikier
 *
 * This file is part of Notehaus.
 * Notehaus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * Notehaus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Notehaus.  If not, see <http://www.gnu.org/licenses/>.
 */

#define _BSD_SOURCE
#include <endian.h>
#include <iconv.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "types.h"

void pack_ubyte_array(void *(*write_fnc)(void *src, size_t n), struct size_data *for_pack) {
  write_fnc(for_pack->data, for_pack->size);
}

void pack_uint16_t(void *(*write_fnc)(void *src, size_t n), struct size_data *for_pack) {
  write_fnc(htole16(for_pack->data), for_pack->size);
}

void pack_uint32_t(void *(*write_fnc)(void *src, size_t n), struct size_data *for_pack) {
  write_fnc(htole32(for_pack->data), for_pack->size);
}


struct size_data *for_ubyte_array(struct size_data **sd, size_t n){
  *sd = malloc(sizeof(struct size_data));
  (*sd)->size = n*sizeof(char);
  (*sd)->data = malloc(n*sizeof(char));
  (*sd)->data_pack_fnc = pack_ubyte_array;
  return *sd;
}
struct size_data *for_uint16_t(struct size_data **sd){
  *sd = malloc(sizeof(struct size_data));
  (*sd)->size = sizeof(uint16_t);
  (*sd)->data = malloc(sizeof(uint16_t));
  (*sd)->data_pack_fnc = pack_uint16_t;
  return *sd;
}
struct size_data *for_uint32_t(struct size_data **sd){
  *sd = malloc(sizeof(struct size_data));
  (*sd)->size = sizeof(uint32_t);
  (*sd)->data = malloc(sizeof(uint32_t));
  (*sd)->data_pack_fnc = pack_uint32_t;
  return *sd;
}
